package models

import "gorm.io/gorm"

type Yuedu struct {
	gorm.Model
	Name    string `gorm:"type:varchar(20)" json:"name"`
	Zuozhe  string `gorm:"type:varchar(30)" json:"zuozhe"`
	Addtime string `gorm:"type:varchar(30)" json:"addtime"`
	Status  int    `gorm:"type:tinyint;default:1;comment:'状态 1已阅读 2未阅读'"`
}

func (y *Yuedu) Del() error {
	return DB.Delete(&y).Error
}

func (y *Yuedu) Get() error {
	return DB.Limit(1).Find(&y).Error
}

func (y *Yuedu) Lists(keyword string, page, pageSize int) (lists []*Yuedu, err error) {
	err = DB.Where("name like ?", "%"+keyword+"%").Scopes(Paginates(page, pageSize)).Find(&lists).Error
	return
}
func Paginates(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
